/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAtendimentoExame.java
 *
 * Created on 27/11/2011, 17:34:49
 */

package oogenda.view;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import oogenda.entidade.Agendamento;
import oogenda.entidade.Atendimento;
import oogenda.entidade.Data;
import oogenda.entidade.Dia;
import oogenda.entidade.Exame;
import oogenda.entidade.Paciente;
import oogenda.util.DadosObjetos;

/**
 *
 * @author Pablo
 */
public class PanelAtendimentoExame extends javax.swing.JPanel {

    private static PanelAtendimentoExame panelAtendimentoExame;

    public static JPanel getPanel() {
        if(panelAtendimentoExame == null) {
            panelAtendimentoExame = new PanelAtendimentoExame();
        }
        panelAtendimentoExame.popularComboPaciente();
        panelAtendimentoExame.popularComboDia();
        panelAtendimentoExame.popularComboHora();
        return panelAtendimentoExame;
    }

    /** Creates new form PanelAtendimentoExame */
    private PanelAtendimentoExame() {
        initComponents();
    }
    
     /**
     * Preenche combobox de paciente
     */
    public void popularComboPaciente() {
        List<Paciente> pacientes = DadosObjetos.pacientes;
        comboPaciente.removeAllItems();
        comboPaciente.addItem("");
        if(pacientes != null) {
            for (Paciente paciente : pacientes) {
                comboPaciente.addItem(paciente);
            }
        }
    }

    public void popularComboDia() {
        List<Data> datas = DadosObjetos.datasExame;
        comboDia.removeAllItems();
        comboDia.addItem("");
        if(datas != null) {
            for (Data data : datas) {
                comboDia.addItem(data);
            }
        }
    }
    
     public void popularComboHora() {
        List<Dia> dias = DadosObjetos.diasExame;
        comboHora.removeAllItems();
        comboHora.addItem("");
        if(dias != null) {
            for (Dia dia : dias) {
                comboHora.addItem(dia);
            }
        }
    }
    
    public void salvar() {
        String nomeExame = campoExame.getText();
        Data data = (Data) comboDia.getSelectedItem();
        Dia dia = (Dia) comboHora.getSelectedItem();
        
        Paciente paciente = (Paciente) comboPaciente.getSelectedItem();
        Exame exameSelecionado = null;
        
        if(paciente == null) {
            JOptionPane.showMessageDialog(null, "O campo Paciente deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }

        if(nomeExame == null || nomeExame.isEmpty()) {
            JOptionPane.showMessageDialog(null, "O campo Exame deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }

        if(data == null) {
            JOptionPane.showMessageDialog(null, "O campo Dia deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }

        if(dia == null) {
            JOptionPane.showMessageDialog(null, "O campo Hora deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
        
        for(Exame exameItem: DadosObjetos.exames) {
            if(exameItem.getNome() != null) {
                if(exameItem.getNome().equals(nomeExame)) {
                    exameSelecionado = exameItem;
                    break;
                }
            }
        }
        
        if(exameSelecionado == null) {
            JOptionPane.showMessageDialog(null, "O exame informado não existe na Clínica Saracura.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
        
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        Atendimento atendimento = new Atendimento();
        try {
            atendimento.setData(format.parse(data + " " + dia));
            //verificar se existe horario na data.
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "A data e hora informadas não sao validas", "Validação", JOptionPane.ERROR_MESSAGE, null);
        }
        atendimento.setCancelado(false);

        Agendamento agendamento = new Agendamento();
        agendamento.setDataHora(atendimento.getData());
        agendamento.setPaciente(paciente);
        agendamento.setM_Agenda(null);

        atendimento.setM_Agendamento(agendamento);

        if(paciente.getAtendimento() == null) {
            paciente.setAtendimento(new ArrayList<Atendimento>());
        }

        paciente.getAtendimento().add(atendimento);

        JOptionPane.showMessageDialog(null, "Dados Salvos", "Sucesso", JOptionPane.INFORMATION_MESSAGE, null);

        //limpar campos
        campoExame.setText(null);
        comboDia.setSelectedItem(null);
        comboHora.setSelectedItem(null);
        comboPaciente.setSelectedItem(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titulo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        comboPaciente = new javax.swing.JComboBox();
        exame = new javax.swing.JLabel();
        campoExame = new javax.swing.JTextField();
        labelDia = new javax.swing.JLabel();
        labelHora = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        comboDia = new javax.swing.JComboBox();
        comboHora = new javax.swing.JComboBox();

        titulo.setFont(new java.awt.Font("Verdana", 1, 14));
        titulo.setText("Agendamento de Exames");

        jLabel1.setText("Paciente:");

        comboPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboPacienteActionPerformed(evt);
            }
        });

        exame.setText("Exame:");

        labelDia.setText("Dia:");

        labelHora.setText("Hora:");

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        comboDia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboDiaActionPerformed(evt);
            }
        });

        comboHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboHoraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titulo, javax.swing.GroupLayout.DEFAULT_SIZE, 908, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboPaciente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(exame)
                            .addComponent(labelDia)
                            .addComponent(labelHora))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(comboDia, 0, 275, Short.MAX_VALUE)
                                .addGap(144, 144, 144))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(comboHora, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1)
                                .addComponent(campoExame, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)))))
                .addContainerGap(425, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titulo)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exame)
                    .addComponent(campoExame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDia)
                    .addComponent(comboDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelHora)
                    .addComponent(comboHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(jButton1)
                .addContainerGap(52, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        salvar();
}//GEN-LAST:event_jButton1ActionPerformed

private void comboPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboPacienteActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboPacienteActionPerformed

private void comboDiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboDiaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboDiaActionPerformed

private void comboHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboHoraActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboHoraActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoExame;
    private javax.swing.JComboBox comboDia;
    private javax.swing.JComboBox comboHora;
    private javax.swing.JComboBox comboPaciente;
    private javax.swing.JLabel exame;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelDia;
    private javax.swing.JLabel labelHora;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables
}
